package com.jwsoft.manager.core.util;

import org.apache.commons.text.similarity.LevenshteinDistance;
import org.lionsoul.jcseg.ISegment;
import org.lionsoul.jcseg.IWord;
import org.lionsoul.jcseg.dic.ADictionary;
import org.lionsoul.jcseg.dic.DictionaryFactory;
import org.lionsoul.jcseg.extractor.SummaryExtractor;
import org.lionsoul.jcseg.extractor.impl.TextRankSummaryExtractor;
import org.lionsoul.jcseg.segmenter.SegmenterConfig;
import org.lionsoul.jcseg.sentence.SentenceSeg;

import java.io.IOException;
import java.io.StringReader;

public class TextUtils {
    private static final SegmenterConfig config;
    private static final ADictionary dic;
    private static final ISegment seg;

    static {
        // 创建配置实例,使用默认配置传递true
        config = new SegmenterConfig(true);
        //创建默认单例词库实现，并且按照config配置加载词库
        dic = DictionaryFactory.createSingletonDictionary(config);
        //依据给定的ADictionary和SegmenterConfig来创建ISegment
        //这里创建的是NLP切分模式,它基于复杂模式添加人名、邮箱、电话等等切分
        seg = ISegment.NLP.factory.create(config, dic);
    }

    /**
     * 分词
     * @param text 文本
     * @return 分词结果
     * @throws IOException io异常
     */
    public static String wordSegmentation(String text) throws IOException {
        //备注：以下代码可以反复调用，seg为非线程安全
        //设置要被分词的文本
        seg.reset(new StringReader(text));
        //获取分词结果
        IWord word;
        StringBuilder sb = new StringBuilder();
        while ((word = seg.next()) != null) {
            sb.append(word.getValue()).append("|");
        }
        return sb.substring(0, sb.length() - 1);
    }

    /**
     * 文字相似度比较
     * @param str1
     * @param str2
     * @return
     */
    public static double calculateSimilarity(String str1, String str2) {
        LevenshteinDistance levenshteinDistance = new LevenshteinDistance();
        double similarity = levenshteinDistance.apply(str1, str2);
        return 1 - (similarity / Math.max(str1.length(), str2.length()));
    }

    /**
     * 截取摘要
     * @param text 文本
     * @param length 摘要长度
     * @return 摘要
     * @throws IOException io异常
     */
    public static String getSummary(String text, int length) throws IOException {
        SummaryExtractor extractor = new TextRankSummaryExtractor(seg, new SentenceSeg());
        return extractor.getSummary(new StringReader(text), length);
    }
    public static void main(String [] args){
        String text = null;
        try {
            text = TextUtils.wordSegmentation("浙江省金华市婺城区长山路181号");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(text);
    }
}
